package pub.guoxin.oops.manager.service.impl;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.vo.ResultVoBuilder;
import pub.guoxin.oops.manager.service.CompanyService;
import pub.guoxin.oops.model.Company;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.repository.CompanyRepository;
import pub.guoxin.oops.repository.UserRepository;

/**
 * Created by guoxin on 17-11-21.
 */
@Slf4j
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private ResultVoBuilder builder;

    @Override
    public void add(Company company, User user) {
        Company save = companyRepository.save(company);
        user.setCompanyId(save.getId());
        user.setAuthorities(Lists.newArrayList(Authority.ROLE_ADMIN));
        userRepository.save(user);
    }

    @Override
    public Company one(String companyId) {
        Company one = companyRepository.findOne(companyId);
        return one;
    }

    @Override
    public Company update(Company company, User user) {
        return companyRepository.save(company);
    }

}
